package com.block.product.service.union;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.mapper.MemberSourceInfoMapper;
import com.block.product.model.dto.UniLoginDTO;
import com.block.product.model.param.ProdApiInfoParam;
import com.block.product.model.result.UnionLoginResult;
import com.block.product.service.IUnionLoginService;
import com.block.product.service.utils.YinDianUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Service
public class WeiXiaoFeiApisService implements IUnionLoginService {

    private static final Logger log = LogFactory.getProductUniLog();

    @Autowired
    private MemberSourceInfoMapper memberSourceInfoMapper;

    @Override
    public UnionLoginResult checkUser(UniLoginDTO uniLoginDTO, ProdApiInfoParam apiInfoParam) throws Exception {
        UnionLoginResult loginResult = new UnionLoginResult();
        loginResult.setSuccess(false);
        String checkUrl = apiInfoParam.getCheckUrl();
        String orgId = apiInfoParam.getApiCode();
        String apiSecret = apiInfoParam.getApiSecret();
        Map<String, Object> bizmMap = new HashMap<>();
        String mobile = uniLoginDTO.getMobile();
        String mobileMd5 = SecureUtil.md5(mobile).toLowerCase();
        bizmMap.put("mobileMd5", mobileMd5);
        bizmMap.put("name", "");
        bizmMap.put("name_md5", "");
        bizmMap.put("CardNo", "");
        bizmMap.put("CardNo_md5", "");
        bizmMap.put("age", 28);
        bizmMap.put("Sex", 0);
        bizmMap.put("city", "");
        bizmMap.put("ip", "");
        bizmMap.put("device_type", 1);
        bizmMap.put("educationLevel", 0);
        bizmMap.put("Position", 3);
        bizmMap.put("support_platform", "");
        bizmMap.put("device_type", "1");
        bizmMap.put("employmentTime", "");
        bizmMap.put("monthlyIncome", 0);
        bizmMap.put("payrollType", 0);
        bizmMap.put("money", 0);
        bizmMap.put("useFor", 0);
        bizmMap.put("loanLongTime", 0);
        bizmMap.put("creditSituation", 0);
        bizmMap.put("IsCredit", 0);
        bizmMap.put("shebao", 0);
        bizmMap.put("gongjijin", 0);
        bizmMap.put("house", 0);
        bizmMap.put("car", 0);
        bizmMap.put("baodan_is", 0);
        bizmMap.put("zmScore", 0);
        bizmMap.put("businessLicense", 0);
        bizmMap.put("huabei", 0);
        bizmMap.put("baitiao", 0);

        long timestamp = System.currentTimeMillis();
        String jsonString = JSON.toJSONString(bizmMap);
        String body =  SecureUtil.aes(apiSecret.getBytes()).encryptBase64(jsonString);
        String sign = "body=" + body + "&orgId=" + orgId + "&timestamp=" + timestamp + "&secret=" + apiSecret;
        String signMd5 = SecureUtil.md5(sign).toLowerCase();
        HashMap<String, Object> map = new HashMap<>();
        map.put("body", body);
        map.put("orgId", orgId);
        map.put("timestamp", timestamp);
        map.put("sign", signMd5);
        log.info("【微消费】撞库开始【queryUrl】{}【bizMap】{}【map】{}", checkUrl,JSON.toJSONString(bizmMap),JSON.toJSONString(map));
        String result = HttpUtil.createPost(checkUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(map))
                .execute().body();
        JSONObject resultObject = JSON.parseObject(result);
        log.info("【微消费】撞库结束【result】{}", resultObject);


        Integer respCode = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");
        //JSONObject data = resultObject.getJSONObject("data");


        loginResult.setCode(respCode.toString());
        loginResult.setMessage(msg);
        loginResult.setResultInfo(result);
        loginResult.setParamInfo(JSON.toJSONString(map));
        if (respCode == 200) {
            loginResult.setSuccess(true);
        }
        return loginResult;
    }

    @Override
    public UnionLoginResult loginUser(UniLoginDTO uniLoginDTO, ProdApiInfoParam apiInfoParam, String resultInfo) throws Exception {

        UnionLoginResult loginResult = new UnionLoginResult();
        loginResult.setSuccess(false);
        String checkUrl = apiInfoParam.getLoginUrl();
        String orgId = apiInfoParam.getApiCode();
        String apiSecret = apiInfoParam.getApiSecret();
        Map<String, Object> bizmMap = new HashMap<>();
        String mobile = uniLoginDTO.getMobile();
        bizmMap.put("mobile", mobile);
        bizmMap.put("name", "");
        bizmMap.put("CardNo", "");
        bizmMap.put("CardNo_md5", "");
        bizmMap.put("age", 28);
        bizmMap.put("Sex", 0);
        bizmMap.put("city", "");
        bizmMap.put("ip", "");
        bizmMap.put("device_type", 1);
        bizmMap.put("educationLevel", 0);
        bizmMap.put("Position", 3);
        bizmMap.put("support_platform", "");
        bizmMap.put("device_type", "1");
        bizmMap.put("employmentTime", "");
        bizmMap.put("monthlyIncome", 0);
        bizmMap.put("payrollType", 0);
        bizmMap.put("money", 0);
        bizmMap.put("useFor", 0);
        bizmMap.put("loanLongTime", 0);
        bizmMap.put("creditSituation", 0);
        bizmMap.put("IsCredit", 0);
        bizmMap.put("shebao", 0);
        bizmMap.put("gongjijin", 0);
        bizmMap.put("house", 0);
        bizmMap.put("car", 0);
        bizmMap.put("baodan_is", 0);
        bizmMap.put("zmScore", 0);
        bizmMap.put("businessLicense", 0);
        bizmMap.put("huabei", 0);
        bizmMap.put("baitiao", 0);
        bizmMap.put("protocolAddress", "");

        long timestamp = System.currentTimeMillis();
        String jsonString = JSON.toJSONString(bizmMap);
        String body =  SecureUtil.aes(apiSecret.getBytes()).encryptBase64(jsonString);
        String sign = "body=" + body + "&orgId=" + orgId + "&timestamp=" + timestamp + "&secret=" + apiSecret;
        String signMd5 = SecureUtil.md5(sign).toLowerCase();
        HashMap<String, Object> map = new HashMap<>();
        map.put("body", body);
        map.put("orgId", orgId);
        map.put("timestamp", timestamp);
        map.put("sign", signMd5);
        log.info("【微消费】进件开始【queryUrl】{}【bizMap】{}【paramMap】{}", checkUrl,JSON.toJSONString(bizmMap));
        String result = HttpUtil.createPost(checkUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(map))
                .execute().body();
        JSONObject resultObject = JSON.parseObject(result);
        log.info("【微消费】进件结束【result】{}", resultObject);

        Integer respCode = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");
        //JSONObject data = resultObject.getJSONObject("data");


        loginResult.setCode(respCode.toString());
        loginResult.setMessage(msg);
        loginResult.setResultInfo(result);
        loginResult.setParamInfo(JSON.toJSONString(map));
        if (respCode == 200) {
            loginResult.setSuccess(true);
        }
        return loginResult;
    }
}
